function insertUsers(connection, user) {
    return new Promise((resolve, reject) => {
        connection.query(`INSERT INTO mto_user SET ?`, user, (e, rows, fields) => {
            if (e) {
                reject({ success: 0, data: { reason: e } })
            } else {
                resolve({ success: 1, data: { userId: rows.insertId } })
            }
        })
    })
}

function insertRole(connection, role, userId) {
    role.userId = userId
    return new Promise((resolve, reject) => {
        connection.query(`insert into  mto_role set ?`, role, (e, rows, fields) => {
            resolve({ success: 0, data: { reason: e } })
            // if (e) {
            //     resolve({ success: 0, data: { reason: e } })
            // } else {
            //     resolve({ success: 1, data: { userId: rows.insertId } })
            // }
        })
    })
}

function getUsers(connection) {
    return new Promise((resolve, reject) => {
        connection.query(`select id,name from mto_user`, null, (e, rows, fields) => {
            if (e) {
                resolve({ success: 0, data: { reason: e } })
            } else {
                resolve({ success: 1, data: rows })
            }
        })
    })
}

module.exports = {
    insertUsers,
    insertRole,
    getUsers
};